package cate.game.role.res.guild.po.treasurehouse;

import cate.common.table.d.GD;
import cate.common.table.d.GDFunc;
import cate.common.table.d.GDGuild;
import cate.common.table.guild.treasurehouse.GuildTreasureHouseItemRow;
import cate.common.util.GameResult;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.res.MixResItem;
import cate.game.role.Role;
import cate.game.role.res.guild.po.my.MyGuildFuncBase;
import cate.game.role.res.guild.po.treasurehouse.msg.GuildTreasureHouseResp;
import cate.game.role.res.guild.po.treasurehouse.msg.GuildTreasureLogResp;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.HashMap;
import java.util.Map;

@NoteClass(value = "我的公会宝库数据")
public class MyGuildTreasureHouse extends MyGuildFuncBase {
    @NoteField("一天放入宝库多少次")
    public int putInTimes;
    @NoteField("某某商品购买了多少次")
    public Map<Integer, Integer> takeOutTimesMap;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (takeOutTimesMap == null) {
            takeOutTimesMap = new HashMap<>();
        }
    }

    /**
     * 个人放入宝库
     *
     * @param tid 宝库物品配置ID
     * @param num 放入数量
     * @return 放入结果
     */
    public GameResult<Void> putTreasureIn(int tid, int num) {
        GameResult<Void> r = new GameResult<>();
        if (!guildFuncOpenCheck()) {
            return r.fail("功能未开启");
        }
        if (num <= 0 || num > 10000000) {
            return r.fail("非法数量");
        }
        GuildTreasureHouseItemRow row = role.getGame().table.guildTreasureHouse.item.get(tid);
        if (row == null) {
            return r.fail("配置错误");
        }
        if (putInTimes >= GDGuild.TreasureHouse.TREASURE_HOUSE_PUT_IN_TIMES_DAY) {
            return r.fail("本日放入次数不足");
        }
        MixRes itemMixRes = new MixRes(row.itemStr).multiply(num);
        GameResult<MixResItem> r_cost = itemMixRes.consumeCheck(role);
        if (!r_cost.ok()) {
            return r.fail(r_cost);
        }
        GameResult<MixRes> r_res = getGuild().treasureHouse.putTreasureIn(role, tid, num, GDFunc.NULL);
        if (!r_res.ok()) {
            return r.fail(r_res);
        }
        itemMixRes.consume(role);
        putInTimes++;
        new MixResAdder().setRes(r_res.data)
                .setDialogReward(true)
                .setSendMailWhenFull(true)
                .exe(role);
        return r.success();
    }

    /**
     * 个人取出宝库
     *
     * @param tid 宝库物品配置ID
     * @param num 放入数量
     * @return 取出结果
     */
    public GameResult<Void> takeTreasureOut(int tid, int num) {
        GameResult<Void> r = new GameResult<>();
        if (!guildFuncOpenCheck()) {
            return r.fail("功能未开启");
        }
        if (num <= 0 || num > 10000000) {
            return r.fail("非法数量");
        }
        GuildTreasureHouseItemRow row = role.getGame().table.guildTreasureHouse.item.get(tid);
        if (row == null) {
            return r.fail("配置错误");
        }
        if (row.dayPurchaseTimeLimit != GD.NEGATIVE) {
            int itemBoughtTime = takeOutTimesMap.getOrDefault(tid, 0);
            if (itemBoughtTime >= row.dayPurchaseTimeLimit) {
                return r.fail("本日已达购买次数上限");
            }
        }
        MixRes cost = new MixRes(row.costStr).multiply(num);
        GameResult<MixResItem> r_cost = cost.consumeCheck(role);
        if (!r_cost.ok()) {
            return r.fail(r_cost);
        }
        GameResult<MixRes> r_item = getGuild().treasureHouse.takeTreasureOut(role, tid, num);
        if (!r_item.ok()) {
            return r.fail(r_item);
        }
        cost.consume(role);
        if (row.dayPurchaseTimeLimit != GD.NEGATIVE) {
            takeOutTimesMap.put(tid, takeOutTimesMap.getOrDefault(tid, 0) + 1);
        }
        new MixResAdder().setRes(r_item.data)
                .setDialogReward(true)
                .setSendMailWhenFull(true)
                .exe(role);
        return r.success();
    }

    @Override
    public int getFuncId() {
        return GDFunc.GuildFunc.TREASURE_HOUSE;
    }

    @Override
    public void onDaySpan(boolean silence) {
        this.putInTimes = 0;
        this.takeOutTimesMap.clear();
        role.sendNow(new GuildTreasureHouseResp(role));
    }

    /**
     * 上线发送
     */
    public void initSend() {
        if (role.getRes().guild.getGuild() == null) {
            return;
        }
        role.sendNow(new GuildTreasureHouseResp(role));
        role.sendNow(new GuildTreasureLogResp(role.getRes().guild.getGuild().treasureHouse.logPO));
    }
}
